﻿package {
	import flash.display.Sprite;
	import flash.events.*;
	import flash.external.ExternalInterface;
	import flash.net.FileReference;
	import flash.net.FileFilter;
	import flash.net.URLRequest;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.net.FileReferenceList;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.net.URLVariables;
	import flash.net.URLRequestMethod;

	public class main extends MovieClip {
		private var file:FileReference;
		private var fileList:FileReferenceList;
		private var upbtn:SimpleButton;
		private var uploadURL:URLRequest;
		private var pendingFiles:Array;
		private var imgfilter:FileFilter;
		private var swffilter:FileFilter;
		private var testTxt:TextField;
		private var count:Number = 0;
		private var variables:URLVariables;
		private var total:Number = 5;
		private var fileSize:Number = 500;
		private var typesArr:Array;
		private var domObj:String;
		public function main() {
			fileList=new FileReferenceList();
			pendingFiles = new Array();
			variables = new URLVariables();
			variables.path = "files/";
			variables.fsize = 2222;
			uploadURL = new URLRequest();
			uploadURL.method = "POST";
			testTxt = this.test_txt;
			this.stage.scaleMode="noScale";
			this.stage.align="TL";


			uploadURL.url = "http://www.jinyisiji.com/test/test.asp";
			uploadURL.data = variables;

			var prmObj:Object = stage.loaderInfo.parameters;
			if (prmObj["upUrl"] != null) {
				uploadURL.url = prmObj["upUrl"];
				domObj="$.files."+prmObj["flashID"];
				testTxt.htmlText = "domObj:"+testTxt.text+prmObj["flashID"]+"<br />";
			}
			upbtn = this.upBtn;
			upbtn.addEventListener(MouseEvent.CLICK,down);
			ExternalInterface.addCallback("deleteFile",deleteFile);
		}
		private function postParameters(file_post:Object):void {
			if (file_post) {
				var key:String;
				for (key in file_post) {
					variables[key] = file_post[key];
					testTxt.htmlText = testTxt.text + "<p> "+key+" 参数： " + file_post[key]+"</p>";
				}
			}
		}
		private function filesAttr(obj:Object):void {
			var key:String;
			var types:String;
			for (key in obj) {
				switch (key) {
					case "total" :
						total = obj[key];
						break;
					case "size" :
						fileSize = obj[key];
						break;
					case "types" :
						types = obj[key];
						break;
				}
			}
			if (types!=null) {
				var tyArr:Array = types.split(":");
				var filef:FileFilter = new FileFilter(String(tyArr[0]),String(tyArr[1]));
				testTxt.htmlText = testTxt.text + "<br />" + tyArr[0]+"---------<br />"+tyArr[1];
				typesArr=[filef];
			}else{
				typesArr = getTypes();
			}
		}
		private function deleteFile(val:String):void {
			var fileName = pendingFiles[val].name;
			pendingFiles[val].cancel();
			pendingFiles.splice(val,1);
			count--;
			testTxt.htmlText = testTxt.text + "<br />" + fileName + " 删除成功 " + val;
		}
		private function setObj(val:String):void{
			testTxt.htmlText = testTxt.text + "<br />-----------" + val + " ---------- ";
			ExternalInterface.call("upfile.alert",val);
			domObj=val;
		}
		private function uploadUrl(val:String):void {
			if(val!=null)uploadURL.url = val;
			testTxt.htmlText = testTxt.text + "<br />上传地址 " + val + " 修改成功 ";
		}
		private function down(e:MouseEvent):void {
			testTxt.htmlText = "<br />对象地址：" + domObj;
			//获取外部参数
			uploadUrl(ExternalInterface.call(domObj+".postUrl"));
			postParameters(ExternalInterface.call(domObj+".postParameters"));
			filesAttr(ExternalInterface.call(domObj+".getAttr"));
			
			
			fileList.browse(typesArr);
			fileList.addEventListener(Event.SELECT, selectHandler);
			fileList.addEventListener(Event.CANCEL, cancelHandler);
		}
		private function selectHandler(event:Event):void {
			if (fileList.fileList.length > total) {
				ExternalInterface.call(domObj+".alert","同时最多只能上传"+total+"个文件");
			} else {
				for (var i:uint = 0; i < fileList.fileList.length; i++) {
					var file:FileReference = FileReference(fileList.fileList[i]);
					if(String(typesArr[0].extension).search(file.type)>0){
						ExternalInterface.call(domObj+".createFile", i,file.name);
						addPendingFile(file);
					}
				}
				uploadCounter();
			}
		}
		private function cancelHandler(event:Event):void {
			testTxt.htmlText=typesArr[0];
		}

		public function getTypes():Array {
			var allTypes:Array = new Array();
			allTypes.push(getImageTypeFilter());
			return allTypes;
		}
		private function addPendingFile(file:FileReference):void {
			pendingFiles.push(file);
		}
		private function getImageTypeFilter():FileFilter {
			return new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png");
		}
		//上传文件
		private function uploadCounter():void {
			if (count<pendingFiles.length) {
				file = FileReference(pendingFiles[count]);
				if (file.size / 1024 < fileSize) {
					file.addEventListener(Event.OPEN, openHandler);
					file.addEventListener(Event.COMPLETE, completeHandler);
					file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, completeUpFile);
					file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
					file.addEventListener(ProgressEvent.PROGRESS, progressHandler);
					file.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
					file.upload(uploadURL,"Filedata");
				} else {
					ExternalInterface.call(domObj+".fileSizeError", count,int(file.size/1024));
					count++;
					uploadCounter();
				}
			}
		}
		private function openHandler(event:Event):void {
			var file:FileReference = FileReference(event.target);
		}
		private function progressHandler(event:ProgressEvent):void {
			var file:FileReference = FileReference(event.target);
			ExternalInterface.call(domObj+".fileSetProgress", count,int(event.bytesLoaded/event.bytesTotal*100));
		}
		private function completeHandler(event:Event):void {
		}
		private function completeUpFile(event:DataEvent):void {
			testTxt.htmlText = testTxt.text + "<br />-----------------<br />" + event.data;
			var file:FileReference = FileReference(event.target);
			testTxt.htmlText = testTxt.text + "<br />" + file.name + " 上传成功";
			var fileSize = file.size;
			if (fileSize<100000) {
				fileSize=int(fileSize/102.4)/10;
			} else {
				fileSize=int(fileSize/1024);
			}
			ExternalInterface.call(domObj+".completeHandler", count,fileSize,event.data);
			ExternalInterface.call(domObj+".completeExport");
			count++;
			uploadCounter();
		}
		private function ioErrorHandler(event:Event):void {
			var file:FileReference = FileReference(event.target);
			ExternalInterface.call(domObj+".fileError", count,"上传失败");
			testTxt.htmlText = testTxt.text + "<br />-----------------<br />" + event;
			trace(file.name+"上传失败");
			count++;
			uploadCounter();
		}
		private function securityErrorHandler(event:Event):void {
			var file:FileReference = FileReference(event.target);
			ExternalInterface.call(domObj+".fileError", count,"传输错误");
			count++;
			uploadCounter();
			trace("传输错误");
		}
	}
}